#
#  Copyright 2008 Garth Williams
#
#  This file is part of CommandPost.
#
#  CommandPost is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  CommandPost is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with CommandPost.  If not, see <http://www.gnu.org/licenses/>.
#

class StagesController < ApplicationController
  
  # GET /stages/new
  # GET /stages/new.xml
  def new
    @stage = Stage.new
    @report_type = ReportType.find(params[:report_type_id])
    @stage.report_type = @report_type

    respond_to do |format|
      format.html { render :layout => false }# new.html.erb
      format.xml  { render :xml => @stage }
    end
  end
  
  # GET /stage/1/edit
  def edit
    @stage = Stage.find(params[:id])
    @report_type = @stage.report_type
    @roles = Role.find(:all, :order => 'name')
    
    render :layout => false
  end

  # POST /stages
  # POST /stages.xml
  def create
    @stage = Stage.new(params[:stage])
    @stage.created_by_user_id = session[:user_id]
    @report_type = ReportType.find(params[:report_type_id])
    @stage.report_type = @report_type
    
    saved_ok = @stage.save
    
    @roles = Role.find(:all, :order => 'name')

    if saved_ok && @report_type.first_stage == nil
      @report_type.first_stage = @stage
      @report_type.created_by_user_id = session[:user_id]
      @report_type.save
    end

    respond_to do |format|
      if saved_ok
        flash[:notice] = 'Stage was successfully created.'
        format.html { render :template => 'report_types/edit', :layout => false }
        format.xml  { render :xml => @stage, :status => :created, :location => @stage }
      else
        format.html { render :action => "new", :layout => false, :status => 500 }
        format.xml  { render :xml => @stage.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /stages/1
  # PUT /stages/1.xml
  def update
    @stage = Stage.find(params[:id])
    @stage.created_by_user_id = session[:user_id]
    @report_type = @stage.report_type
    @roles = Role.find(:all, :order => 'name')

    respond_to do |format|
      if @stage.update_attributes(params[:stage])
        flash[:notice] = 'Stage was successfully updated.'
        format.html { render :template => 'report_types/edit', :layout => false }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit", :layout => false, :status => 500 }
        format.xml  { render :xml => @stage.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /stages/1
  # DELETE /stages/1.xml
  def destroy
    @stage = Stage.find(params[:id])
    @stage.created_by_user_id = session[:user_id]
    @stage.destroy
    @report_type = @stage.report_type
    if @report_type.first_stage_id == @stage.id
      if @report_type.stages.length == 0
        #no stages left, so set furst stage to nil
        @report_type.first_stage_id = nil
      else
        #set the first stage to any of the remaining stages
        @report_type.first_stage = @report_type.stages[0]
      end
      @report_type.created_by_user_id = session[:user_id]
      @report_type.save
    end
    @roles = Role.find(:all, :order => 'name')

    respond_to do |format|
      format.html { render :template => 'report_types/edit', :layout => false }
      format.xml  { head :ok }
    end
  end
end
